<?php 
// Nguyen Binh
// suongmumc@gmail.com
// update 04/2009

defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
$tpl=new TemplatePower("skin/sk_news.htm");
$tpl->prepare();

//header
$title="Quản lý chuyên mục";
$root="Trang tin tức";
$par_page="sr_news";
$cat_name       ="danh m&#7909;c";
$category       =$fix."cat".$lang;
$id_category    ="id_cat";
$item_name      ="bài viết";
$item           =$fix."news".$lang;
$id_item        ="id_news";
$maxpage=8;
$maxitem=20;


$id=intval($_GET['id']);

// cat id
if($_GET['pid'])
{
	$pid=$_GET['pid'];
}
elseif($_POST['pid'])
{
	$pid=$_POST['pid'];
}
else
{
	$pid=0;
}

// numpage cat
if($_GET['p'])
{
	$p=$_GET['p'];
}
elseif($_POST['p'])
{
	$p=$_POST['p'];
}
//numpage item
if($_GET['pitem'])
{
	$pitem=$_GET['pitem'];
}
elseif($_POST['pitem'])
{
	$pitem=$_POST['pitem'];
}

$id=intval($_REQUEST['id']);

// global 
$tpl->assignGlobal('title',$title);
$tpl->assignGlobal('cat_name',$cat_name);
$tpl->assignGlobal('item_name',$item_name);

$tpl->assignGlobal('p',$p);
$tpl->assignGlobal('pitem',$pitem);
$tpl->assignGlobal('pid',$pid);
$tpl->assignGlobal('par_page',$par_page);
// path page
$navigator="<a href='?page=".$par_page."&code=00'>$root</a>";
$ct=new class_tree($pid);
$ct->get_cat_tree(0,$category,$id_category);	
$ct->get_cat_string_admin($pid,$par_page,$category,$id_category);
$navigator.=$catstring2;								
$tpl->assignGlobal('pathpage',$navigator);
if(!$_GET['code'] || $_GET['code']=='00'){
	show_list_category();
	show_list_item();
}
// show form addnew cat
if($_GET['code']=='01'){
	$tpl->newBlock("addnew_cat");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	
	//----------
	$oFCKeditor = new FCKeditor("content") ;
	$oFCKeditor->BasePath = '../FCKeditor/';
	$oFCKeditor->Value = $rs['content'];
	$oFCKeditor->Height='250';
	$tpl->assign("content",$oFCKeditor->CreateHtml());
	
	$tpl->assign("thu_tu",auto_order($category,'thu_tu',$pid));
	show_list_category();
}
// insert new cat
if($_GET['code']=='02'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");

		$a['keyword']		=compile_post("keyword");
		$a['description']	=compile_post("description");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['inhome']		=intval(compile_post("inhome"));
		$a['active']		=intval(compile_post("active"));
		$a['parentid']		=intval(compile_post("parentid"));
		$a['content']		=$_POST['content'];
	
		if($_FILES['image']['size']){
			$image=uploadimage("image","no");
			$a['image']			=$image['image'];
		}
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$category." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		if($DB) {
			write_link();
			message("Th&#234;m m&#7899;i $item_name th&#224;nh c&#244;ng !");
		}
	}
	show_list_category();
}
// show form update cat;
if($_GET['code']=='03'){
	$tpl->newBlock("update_cat");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	
	
	$sql="SELECT * FROM $category WHERE $id_category=$id";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		$tpl->assign("content",$rs['content']);
		$tpl->assign("keyword",$rs['keyword']);
		$tpl->assign("description",$rs['description']);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		if($rs['active']==1) $tpl->assign("active","checked");		
		if($rs['inhome']==1) $tpl->assign("inhome","checked");		
		$tpl->assign("id",$rs[$id_category]);
		//----------
		$oFCKeditor = new FCKeditor("content") ;
		$oFCKeditor->BasePath = '../FCKeditor/';
		$oFCKeditor->Value = $rs['content'];
		$oFCKeditor->Height='250';
		$tpl->assign("content",$oFCKeditor->CreateHtml());
	}
	
	show_list_category();
}
// update cat
if($_GET['code']=='04'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['keyword']		=compile_post("keyword");
		$a['description']	=compile_post("description");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['active']		=intval(compile_post("active"));
		$a['inhome']		=intval(compile_post("inhome"));
		$a['parentid']		=intval(compile_post("parentid"));
		$a['content']		=$_POST['content'];
		if($_FILES['image']['size'] || $_POST['xoa_anh']==1){
			$sql="SELECT * FROM $category WHERE $id_category=".$id; 
			$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				deleteimage($rs['image']);
				$a['image']='';
			}
			$image=uploadimage("image");
			$a['image']=$image['image'];
		}
		
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE ".$category." SET ".$b." WHERE ".$id_category."=".$id;
		$DB->query($sql);
		if ($DB) {
			write_link();
			message("Sửa chữa thành công !");
		}
	}
	show_list_category();
	show_list_item();

}
// change order active
if($_GET['code']=='05'){
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'p');

	change_active_order_paging($category,$id_category,$db['db']);
	show_list_category();
	show_list_item();

}
// delete category
if($_GET['code']=='06'){
	
	if(checkDelCat($id)){
		$sql="SELECT * FROM $category WHERE $id_category=".$id;
		$db=$DB->query($sql);
		if($rs=mysql_fetch_array($db)){
			$sql1="DELETE FROM $category WHERE $id_category=".$id;
			
			$db=$DB->query($sql1);
			if($db){
				write_link();
				 message("Đã xóa xong !");
			}
			else {
				message("Lỗi không xóa được");
			}
		}
	}
	show_list_category();
	show_list_item();

}
// show form addnew item
if($_GET['code']=='11'){
	$tpl->newBlock("addnew_item");
	
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	//-------
	$oFCKeditor = new FCKeditor("intro") ;
	$oFCKeditor->BasePath = '../FCKeditor/';
	$oFCKeditor->Value = $rs['intro'];
	$oFCKeditor->ToolbarSet = 'Basic';
	$oFCKeditor->Width='100%';
	$oFCKeditor->Height='150';
	$tpl->assign("intro",$oFCKeditor->CreateHtml());
	
	//----------
	$oFCKeditor = new FCKeditor("content") ;
	$oFCKeditor->BasePath = '../FCKeditor/';
	$oFCKeditor->Value = $rs['content'];
	$tpl->assign("content",$oFCKeditor->CreateHtml());
	$tpl->assign("ngay_dang",date('d/m/Y H:i',time() ));
	$tpl->assign("thu_tu",auto_orders($item,'thu_tu',$pid,$id_category));
	
	show_list_item();
	$tpl->newBlock("offsetupload");
}
// insert new item 
if($_GET['code']=='12'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		
		$a['imgshowdetail']	=intval(compile_post("imgshowdetail"));
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['hit']		=intval(compile_post("hit"));
		$a['hot']		=intval(compile_post("hot"));
		$a['inhome']		=intval(compile_post("inhome"));
		
		$a['active']		=intval(compile_post("active"));
		$a[$id_category]	=intval(compile_post("parentid"));
		$a['intro']			=$_POST['intro'];
		$a['content']		=$_POST['content'];
		//$a['ngay_dang']		=time()+$CONFIG['time_offset'];
		if(compile_post('ngay_dang')!=''){
			$a['ngay_dang']		=strip_date_time(compile_post('ngay_dang'));
		}
		$a['id_user']=$my['id'];
		// xu ly anh
		
		$a['image']			=compile_post('image');
		$a['normal_image']	=compile_post('normal_image');
		$a['small_image']	=compile_post('small_image');
		
	}
	$b=$DB->compile_db_insert_string($a);
	$sql="INSERT INTO ".$item." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
	$DB->query($sql);
	if($DB) message("Th&#234;m m&#7899;i $item_name th&#224;nh c&#244;ng !");
	show_list_item();
}
// show form update item
if($_GET['code']=='13'){
	$tpl->newBlock("update_item");
	$tpl->assign("id",$id);
	
	
	$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		$tpl->assign("hit",intval($rs['hit']));
		
		$info['parentid1']=$rs[$id_category];
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);		
		if($rs['small_image'])
		{
			$tpl->assign('image',"<img src='".'../'.$CONFIG['upload_image_path'].$rs['small_image']."' ><br><input type='checkbox' name='xoa_anh' value='1' class='noborder'>&nbsp;X&#243;a &#7843;nh<br>");		
		}
		//-------
		$oFCKeditor = new FCKeditor("intro") ;
		$oFCKeditor->BasePath = '../FCKeditor/';
		$oFCKeditor->Value = $rs['intro'];
		$oFCKeditor->ToolbarSet = 'Basic';
		$oFCKeditor->Width='100%';
		$oFCKeditor->Height='150';
		$tpl->assign("intro",$oFCKeditor->CreateHtml());
		
		//----------
		$oFCKeditor = new FCKeditor("content") ;
		$oFCKeditor->BasePath = '../FCKeditor/';
		$oFCKeditor->Value = $rs['content'];
		$tpl->assign("content",$oFCKeditor->CreateHtml());
		
		$tpl->assign("thu_tu",$rs['thu_tu']);
		if($rs['ngay_dang']>0){
			$tpl->assign("ngay_dang",date('d/m/Y H:i',$rs['ngay_dang'] ));
		}
		if($rs['imgshowdetail']==1) $tpl->assign("imgshowdetail","checked");
		if($rs['active']==1) $tpl->assign("active","checked");
		if($rs['hot']==1) $tpl->assign("hot","checked");
		if($rs['inhome']==1) $tpl->assign("inhome","checked");
	
	}
	show_list_item();
	$tpl->newBlock("offsetupload");
}
// update item
if($_GET['code']=='14'){
	$in_name=	compile_post("name");
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['imgshowdetail']	=intval(compile_post("imgshowdetail"));
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['hit']		=intval(compile_post("hit"));
		$a['hot']		=intval(compile_post("hot"));
		$a['inhome']		=intval(compile_post("inhome"));
		$a['active']		=intval(compile_post("active"));
		$a[$id_category]	=intval(compile_post("parentid"));
		$a['intro']			=$_POST['intro'];
		$a['content']		=$_POST['content'];

	//	$a['ngay_dang']		=time()+$CONFIG['time_offset'];
		if(compile_post('ngay_dang')!=''){
			$a['ngay_dang']		=strip_date_time(compile_post('ngay_dang'));
		}else{
			$a['ngay_dang']	=0;
		}
		// xu ly anh
		

		if($_POST['xoa_anh']==1 || compile_post('image')){
			$sql="SELECT * FROM $item WHERE $id_item=".$id; 
			$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);
				$a['image']='';
				$a['normal_image']='';
				$a['small_image']='';
			}
			if(compile_post('image')){
				$a['image']			=compile_post('image');
				$a['normal_image']	=compile_post('normal_image');
				$a['small_image']	=compile_post('small_image');		
			}

		}
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE ".$item." SET ".$b." WHERE ".$id_item."=".$id;
		$db=$DB->query($sql);
		if($db) message("Sửa chữa thành công !");
		else message("Lỗi không sửa được !");
	}
	show_list_item();
}
// change order active - order
if($_GET['code']=='15'){
	$sql="SELECT ".$item.".*,".$fix."users.name as user_name, ".$fix."users.username as username FROM $item LEFT JOIN ".$fix."users ON(".$item.".id_user=".$fix."users.id_users) WHERE $id_category=$pid ORDER BY thu_tu DESC, $id_item DESC";
	$db=phan_trang($pitem,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'pitem');
	change_active_order_paging($item,$id_item,$db['db']);
	show_list_category();
	show_list_item();
}
// delete item 
if($_GET['code']=='16'){
	$sql="SELECT * FROM $item WHERE $id_item=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);

		$sql1="DELETE FROM $item WHERE $id_item=".$rs[$id_item];
		$db=$DB->query($sql1);
		if($db) message("Đã xóa xong !");
		else message("Lỗi không xóa được !");
	}
	show_list_category();
	show_list_item();
}
// delete multi item
if($_GET['code']=='17'){
	//$id_items=compile_post("del1_id");
	$id_items=$_POST['delallitem'];
	if($id_items);
	$list_id=0;
	foreach($id_items as $idit){
		$list_id.=','.$idit;
	}
	$sql1="SELECT * FROM $item WHERE $id_item IN (".$list_id.")";
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		deleteimage($rs1['image'],$rs1['normal_image'],$rs1['small_image']);
	}
	$sql="DELETE FROM $item WHERE $id_item IN (".$list_id.")";
	$db=$DB->query($sql);
	if($db) message("Đã xóa xong !");
	else message("Lỗi không xóa được !");
	
	show_list_category();
	show_list_item();
}
// search item
if($_GET['code']=='18'){
	
	$keyword=clean_value($_REQUEST["keyword"]);
	$parentid=intval(compile_post("parentid"));
	$dk=" $id_category IN(".Get_ID_Cat($parentid).") AND $item.name like'%".$keyword."%' ";
	 show_list_item($dk);
}

// show list category and item
function show_list_category(){
	global $DB,$tpl,$category,$id_category,$item,$id_item,$pid,$p,$pitem,$maxpage,$maxitem,$par_page,$fix;
	$tpl->newBlock("show_category");
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'p');
	$i=0;
	$tpl->assignglobal("ncat",mysql_num_rows($db['db']));
	while($rs=mysql_fetch_array($db['db'])){
		$tpl->newBlock("list_cat");
		$i++;
		$tpl->assign("i",$i);
		$tpl->assign("name",$rs['name']);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}
		$tpl->assign("id_category",$rs[$id_category]);
		$tpl->assign("linkcat","?page=$par_page&pid=".$rs[$id_category]);
		$tpl->assign("link_update","?page=$par_page&pid=$pid&id=$rs[$id_category]&code=03&p=$p&pitem=$pitem");

		$tpl->assign("link_del","?page=$par_page&pid=$pid&id=$rs[$id_category]&code=06&p=$p&pitem=$pitem");

	}
	$tpl->assignGlobal("page_cat",$db['cac_trang']);
}
// show list item
function show_list_item($dk=''){
	global $DB,$tpl,$category,$id_category,$item,$id_item,$pid,$p,$pitem,$maxpage,$maxitem,$par_page,$tree,$fix,$CONFIG,$keyword;
	$keyword=clean_value($_REQUEST["keyword"]);
	$code=$_GET['code'];
	$tpl->newBlock("show_item");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px"  class="dropdownlist">';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	if($dk==''){
		$dk="$id_category=$pid";
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$sql="SELECT ".$item.".*,".$fix."users.name as user_name, ".$fix."users.username as username FROM $item LEFT JOIN ".$fix."users ON(".$item.".id_user=".$fix."users.id_users) WHERE  $dk ORDER BY thu_tu DESC, $id_item DESC";
	$db=phan_trang($pitem,"?page=$par_page&pid=$pid&code=$code&keyword=$keyword",$sql,$maxpage,$maxitem,'pitem');
	$i=0;
	$tpl->assignGlobal("nitem",mysql_num_rows($db['db']));
	while($rs=mysql_fetch_array($db['db'])){
		$tpl->newBlock("list_item");
		$i++;
		$tpl->assign("name",$rs['name']);

		$tpl->assign("i",$i);
		$tpl->assign("id",$rs[$id_item]);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		$tpl->assign("hit",intval($rs['hit']));
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}
		if($rs['small_image'])
		{
			$tpl->assign('image',"<img border='0' src='".'../'.$CONFIG['upload_image_path'].$rs['small_image']."' align='left' width='60' height='40' style='margin-right:5px'>");		
		}
		$tpl->assign("user_name",$rs['user_name']);
		$tpl->assign("username",$rs['username']);
		$tpl->assign("creatdate",date('d/m/Y | H:i',$rs['ngay_dang']));
		$tpl->assign("link_edit","?page=$par_page&pid=$pid&id=$rs[$id_item]&code=13&pitem=$pitem&p=$p");
	}
	$tpl->assignGlobal("pages_items",$db['cac_trang']);
}

function checkDelCat($id)
{
	global $category,$cat_name,$item_name,$item,$id_category,$DB;
	$sql="select * from ".$category." where parentid = ".$id;
	$db=$DB->query($sql);	
	if(mysql_num_rows($db)>0)
	{
		message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $cat_name con b&#234;n trong.h&#227;y x&#243;a c&#225;c $cat_name con tr&#432;&#7899;c!");
		return false;
	}
	else
	{
		$sql1="select * from ".$item." where ".$id_category." = ".$id;
		$db1=$DB->query($sql1);	
		if(mysql_num_rows($db1)>0)
		{
			message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $item_name  b&#234;n trong.h&#227;y x&#243;a c&#225;c $item_name con tr&#432;&#7899;c!");
			return false;
		}
		else
		{
			return true;
		}
	}
}

$tpl->printToScreen();
function GetTitle($idc){
	global $DB,$tpl;
	$idc=intval($idc);
	$sql="SELECT * FROM cat WHERE active=1 AND id_cat=$idc";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("_ROOT.modulename",$rs['name'])	;
	}
}
function Get_ID_Cat($id){
	global $DB,$category, $id_category;
	$id=intval($id);
	$list_id=$id;
	$sql="SELECT * FROM $category WHERE parentid=$id";
	$db=$DB->query($sql);
	while($rs=mysql_fetch_array($db)){
		$list_id.=",".Get_ID_Cat($rs[$id_category]);
	}
	return $list_id;
	
}
?>
